package org.grow;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import processing.event.KeyEvent;
import processing.event.MouseEvent;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

public class Rocket extends PApplet {

    public static void main(String[] args) {
        String[] arg={"Rocket"};
        Rocket rocket = new Rocket();
        PApplet.runSketch(arg,rocket);
    }
// 图片原始资源 n个星球和一个火箭
    PImage[] planetsImg;
    PImage rocketImg ;
// 图片在本项目中显示的具体实际位置信息 其实可以做到一个素材出现n次 此处代表每一次的信息
//    一个火箭位置大小 和n个星球位置大小
//    你原来写的xy其实就是火箭的xy位置 rocketPosition.x rocketPosition.y
    Character rocketPosition;
//    ArrayList 跟数组[] 一样,只是长度可变 可以一直加入元素
    ArrayList<Character> planetsPositions;

    Tool tool;
//    float x; 集成到rocketPosition变量中去了
//    float y;
    int opSpeed;
//    float nSpeed=0.5f;
    int circlenum= 20;
//    float[] xpos = new float[circlenum];
//    float[] ypos = new float[circlenum];

    @Override
    public void settings() {
        size(800,800);

        opSpeed = 5;
        tool = new Tool();
//        第一步加载资源图片 初始化数据
//        if use processing3.4 editor
//        String prefix = "";

        String prefix = "/home/xieweig/IdeaProjects/rocket/resources/";
        rocketImg = loadImage(prefix+"rocket_cut.png");
        rocketPosition = new Character(width/2-70,height-140,50,50*rocketImg.height/rocketImg.width);
        planetsImg = new PImage[4];
        for (int i = 0; i < planetsImg.length; i++) {
            planetsImg[i]=loadImage(prefix+"planet"+(i+1)+"_cut.png");

        }
//        此时还是简单的一个素材出现一次 这里只填充占位
//        for (int i = 0; i < 4; i++) {
//            planetsPositions.add(null);
//        }
//        初始化状态行星的位置
        planetsPositions = new ArrayList<>();

        planetsPositions.add(new Character(50,550,50,50*planetsImg[0].height/planetsImg[0].width));
        planetsPositions.add(new Character(300,100,60,60*planetsImg[1].height/planetsImg[1].width));
        planetsPositions.add(new Character(250,200,50,50*planetsImg[2].height/planetsImg[2].width));
        planetsPositions.add(new Character(400,300,100,100*planetsImg[3].height/planetsImg[3].width));


    }

    @Override
    public void draw() {
        imageMode(CENTER);
        background(0);
        strokeWeight(0);
        fill(255);
//        for (int i = 0; i < circlenum; i++) {
//
//            xpos[i] = random(width);
//            ypos[i] = random(height);
//            circle (xpos[i],ypos[i],5);
//        }
//        随机雪花如果只是个装饰 不打算后续检测碰撞等 不需要赋值那么多引用例如xpos 看着乱
        for (int i = 0; i < circlenum; i++) {
            circle(random(width),random(height),5);
        }


        strokeWeight(5);
        image(rocketImg,rocketPosition.x,rocketPosition.y,rocketPosition.w,rocketPosition.h);
        noFill();
        stroke(0,200,0);
//            碰撞检测 使用的是
        rectMode(CENTER);
        rect(rocketPosition.x,rocketPosition.y,rocketPosition.w,rocketPosition.h);

        for (int i = 0; i <planetsPositions.size() ; i++) {
            Character c = planetsPositions.get(i);

//            核心显示代码

            image(planetsImg[i],c.x,c.y,c.w,c.h);
//            更新行星的位置大小 以便进行碰撞检测

//            planetsPositions.set(i,new Character(c.x,c.y,c.w,c.h));
//            开发测试状态使用
            noFill();
            stroke(0,0,50*(i+1));
//            碰撞检测 使用的是
            rectMode(CENTER);
            rect(c.x,c.y,c.w,c.h);
        }

//        image(img2,m,n,50,50*img2.height/img2.width);
//        positions.set(1,new Character(m,n,50,50));
//        //fill(0,0,50);
//        noFill();
//        stroke(0,0,50);
//        rect(m,n,50,50);
//        image(img3,m+300,n-100,60,60*img3.height/img3.width);
//        positions.set(2,new Character(m+300,n-100,60,60));
//        noFill();
//        stroke(0,0,100);
//        rect(m,n,50,50);
//        rect(m+300,n-100,60,60);
//        image(img4,m+120,n+200,50,50*img4.height/img4.width);
//        positions.set(3,new Character(m+120,n+200,50,50));
//        noFill();
//        stroke(0,0,150);
//        rect(m,n,50,50);
//        rect(m+120,n+200,50,50);
//        image(img5,m+400,n-100,80,80*img5.height/img5.width);
//        positions.set(4,new Character(m+400,n-100,80,80));
//        noFill();
//        stroke(0,0,200);
//        rect(m,n,50,50);
//        rect(m+400,n-100,80,80);
//collision
//        System.out.println(planetsPositions.get(3));
//        System.out.println(rocketPosition);
        for(int i = 0; i<planetsPositions.size();i++){
            Character planet = planetsPositions.get(i);
            boolean isCollison = tool.rectCollision(rocketPosition.x,rocketPosition.y
                    ,planet.x,planet.y
                    ,rocketPosition.w,rocketPosition.h
                    ,planet.w,planet.h);
            if(isCollison){
//                下方可以写具体方法 这就是你的舞台
                AfterCollision();
            }
        }

        if(rocketPosition.y > 140*rocketImg.height/rocketImg.width){
            rocketPosition.y -= 1.5f;
        }
        else{
            rocketPosition.y = 660;
        }

//        让一个星球动起来 运动过程中的碰撞也自然会检测到 控制每个planet的xy变化 还可以控制他的wh变化
//        像雷电 这种游戏 就是运动的子弹
        planetsPositions.get(0).x++;
        planetsPositions.get(0).y--;

    }

    @Override
    public void keyPressed(KeyEvent event) {
        super.keyPressed(event);
        if (keyCode == LEFT){
            if(rocketPosition.x>opSpeed){rocketPosition.x -= opSpeed;}
        }
        if (keyCode == RIGHT){
            if(rocketPosition.x<width-opSpeed){rocketPosition.x += opSpeed;}
        }
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        if (!isLooping()){


        rocketPosition.y = 660;
        planetsPositions.get(0).x=50;
        planetsPositions.get(0).y=550;

        loop();
        }
    }

    void AfterCollision(){
        fill(255,0,0);
        rect(10,10,50,50);
        fill(255,250,250);
        text("游戏结束,随意点击鼠标左键,从新开始",100,20);
        noLoop();
    }

}

class Character{
    float x;
    float y;
    int w;
    int h;
    //character position x y ,width w ,height h
    public Character(float x, float y, int w, int h){
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    @Override
    public String toString() {
        return "Character{" +
                "x=" + x +
                ", y=" + y +
                ", w=" + w +
                ", h=" + h +
                '}';
    }
}